package com.Lubaichen.Wiki.controller;

import com.Lubaichen.Wiki.Pojo.Content;
import com.Lubaichen.Wiki.req.ContentQueryReq;
import com.Lubaichen.Wiki.req.DocQueryReq;
import com.Lubaichen.Wiki.req.DocSaveReq;
import com.Lubaichen.Wiki.resp.DocQueryResp;
import com.Lubaichen.Wiki.resp.CommonResp;
import com.Lubaichen.Wiki.resp.PageResp;
import com.Lubaichen.Wiki.service.DocService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Lubaichen
 * @date 2022年03月19日 19:52
 */
@RestController
@RequestMapping("/doc")
public class DocController {
    @Resource
    private DocService docService;

    @GetMapping("/list") //Spring会自动将参数映射到类属性
    public CommonResp docList(@Valid DocQueryReq req) {
        CommonResp<PageResp<DocQueryResp>> resp = new CommonResp<>();
        PageResp<DocQueryResp> list = docService.list(req);
        resp.setContent(list);
        return resp;
    }

//    @GetMapping("/all")
//    public CommonResp docAll1(DocQueryReq req) {
//        CommonResp<List<DocQueryResp>> commonResp = new CommonResp<>();
//        List<DocQueryResp> all = docService.all(req);
//        commonResp.setContent(all);
//        return commonResp;
//    }
//
//    @GetMapping("/all/{id}")
//    public CommonResp docAll(DocQueryReq req) {
//        CommonResp<List<DocQueryResp>> commonResp = new CommonResp<>();
//        List<DocQueryResp> all = docService.all(req);
//        commonResp.setContent(all);
//        return commonResp;
//    }

    @GetMapping("/all/{ebookId}")
    public CommonResp docAll2(@PathVariable Long ebookId) {
        CommonResp<List<DocQueryResp>> commonResp = new CommonResp<>();
        List<DocQueryResp> all = docService.all(ebookId);
        commonResp.setContent(all);
        return commonResp;
    }

    @PostMapping("/save")
    public CommonResp docSave(@Valid @RequestBody DocSaveReq req) {
        CommonResp resp = new CommonResp<>();
        docService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{idsStr}")
    public CommonResp deleteEbookById(@PathVariable String idsStr) {
        CommonResp resp = new CommonResp<>();
        if (!ObjectUtils.isEmpty(idsStr)) {
            List<String> strings = Arrays.asList(idsStr.split(","));
            Long[] ids = new Long[strings.size()];
            int i = 0;
            for (String str : strings) {
                ids[i++] = Long.parseLong(str);
            }
            List<Long> listId = Arrays.asList(ids);
            docService.deleteEbookById(listId);
        }
        return resp;
    }

    @GetMapping("/search")
    public CommonResp docSearch(DocQueryReq req) {
        CommonResp<List<DocQueryResp>> commonResp = new CommonResp<>();
        List<DocQueryResp> list = docService.search(req);
        commonResp.setContent(list);
        return commonResp;
    }


    @GetMapping("/find-content/{id}")
    public CommonResp docContentSearch(@PathVariable Long id) {
        CommonResp<String> commonResp = new CommonResp<>();
        String content = docService.findContentById(id);
        commonResp.setContent(content);
        return commonResp;
    }

    @GetMapping("/vote/{id}")
    public CommonResp voteIncrement(@PathVariable Long id) {
        CommonResp<Object> commonResp = new CommonResp<>();
        docService.voteIncrement(id);
        return commonResp;
    }
}
